package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.gc.business.domain.CommunityManage;
import com.gc.business.domain.ElevatorInfo;
import com.gc.business.domain.UpkeepPlan;
import com.gc.business.service.ICommunityManageService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.business.service.IUpkeepPlanService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysDictDataService;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 电梯 控制层
 *
 * @author liujinteng
 * @date 2020-11-13
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorInfo")
public class ElevatorInfoController extends BaseController {
    private String prefix = "business/elevatorInfo";

    private final IElevatorInfoService elevatorInfoService;

    private final ICommunityManageService communityManageService;

    private final IUpkeepPlanService upkeepPlanService;

    private final ISysDictDataService dictDataService;

    private final ISysUserService userService;

    @Autowired
    public ElevatorInfoController(IElevatorInfoService elevatorInfoService,
                                  ICommunityManageService communityManageService,
                                  ISysDictDataService dictDataService,
                                  ISysUserService userService,
                                  IUpkeepPlanService upkeepPlanService) {
        this.elevatorInfoService = elevatorInfoService;
        this.communityManageService = communityManageService;
        this.upkeepPlanService = upkeepPlanService;
        this.dictDataService = dictDataService;
        this.userService = userService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:elevatorInfo:view")
    @GetMapping
    public String elevatorInfo() {
        return prefix + "/elevatorInfo";
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:elevatorInfo:view")
    @GetMapping("/management")
    public String elevatorManagement() {
        return prefix + "/elevatorManagement";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增直梯
     */
    @GetMapping("/addStraightLadder")
    public String addStraightLadder(ModelMap modelMap) {
        List<CommunityManage> communityManageList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        modelMap.put("communityManageList", communityManageList);
        return prefix + "/addStraightLadder";
    }

    /**
     * 新增扶梯
     */
    @GetMapping("/addEscalator")
    public String addEscalator(ModelMap modelMap) {
        List<CommunityManage> communityManageList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        modelMap.put("communityManageList", communityManageList);
        return prefix + "/addEscalator";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{elevatorId}")
    public String edit(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        mmap.put("elevatorInfo", elevatorInfo);
        return prefix + "/edit";
    }

    /**
     * 编辑直梯
     */
    @GetMapping("/editStraightLadder/{elevatorId}")
    public String editStraightLadder(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        if (StringUtils.isNotEmpty(elevatorInfo.getLongitude())) {
            elevatorInfo.setLongitude(elevatorInfo.getLatitude() + "," + elevatorInfo.getLongitude());
        }
        mmap.put("elevatorInfo", elevatorInfo);
        List<CommunityManage> communityManageList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        mmap.put("communityManageList", communityManageList);
        return prefix + "/editStraightLadder";
    }

    /**
     * 编辑扶梯
     */
    @GetMapping("/editEscalator/{elevatorId}")
    public String editEscalator(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        if (StringUtils.isNotEmpty(elevatorInfo.getLongitude())) {
            elevatorInfo.setLongitude(elevatorInfo.getLatitude() + "," + elevatorInfo.getLongitude());
        }
        mmap.put("elevatorInfo", elevatorInfo);
        List<CommunityManage> communityManageList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        mmap.put("communityManageList", communityManageList);
        return prefix + "/editEscalator";
    }

    /**
     * 安装信息
     */
    @GetMapping("/installInfo/{elevatorId}")
    public String installInfo(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        mmap.put("elevatorInfo", elevatorInfo);
        return prefix + "/installInfo";
    }

    /**
     * 出厂信息
     */
    @GetMapping("/exFactoryInfo/{elevatorId}")
    public String exFactoryInfo(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        mmap.put("elevatorInfo", elevatorInfo);
        return prefix + "/exFactoryInfo";
    }

    /**
     * 电梯基础信息
     */
    @GetMapping("/elevatorBaseInfo/{elevatorId}")
    public String elevatorBaseInfo(@PathVariable("elevatorId") Long elevatorId, ModelMap mmap) {
        ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorId);
        if (StringUtils.isNotEmpty(elevatorInfo.getLayerNum())) {
            elevatorInfo.setLayerNum(elevatorInfo.getLayerNum() + "层" + elevatorInfo.getStandingNum() + "站");
        }
        if (StringUtils.isNotEmpty(elevatorInfo.getLongitude())) {
            elevatorInfo.setLongitude(elevatorInfo.getLatitude() + "," + elevatorInfo.getLongitude());
        }
        if (StringUtils.isNotEmpty(elevatorInfo.getBuildType())) {
            elevatorInfo.setBuildType(dictDataService.selectDictDataByType("business_build_type", elevatorInfo.getBuildType()));
        }
        if (StringUtils.isNotEmpty(elevatorInfo.getStraightElevatorType())) {
            if (elevatorInfo.getStraightElevatorType().equals("0")) {
                elevatorInfo.setStraightElevatorType("液压电梯");
            } else if (elevatorInfo.getStraightElevatorType().equals("1")) {
                elevatorInfo.setStraightElevatorType("曳引电梯");
            } else if (elevatorInfo.getStraightElevatorType().equals("2")) {
                elevatorInfo.setStraightElevatorType("杂物电梯");
            }
        }
        if (StringUtils.isNotEmpty(elevatorInfo.getEscalatorType())) {
            if (elevatorInfo.getEscalatorType().equals("0")) {
                elevatorInfo.setEscalatorType("自动扶梯");
            }
        }
        mmap.put("elevatorInfo", elevatorInfo);
        return prefix + "/elevatorBaseInfo";
    }

    /**
     * 查询电梯列表
     */
    @RequiresPermissions("business:elevatorInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(ElevatorInfo elevatorInfo) {
        List<CommunityManage> communityList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        if (CollUtil.isEmpty(communityList)){
            return Result.success();
        }
        List<Long> collect = communityList.stream().map(CommunityManage::getCommunityId).collect(Collectors.toList());
        startPage();
        List<ElevatorInfo> list = elevatorInfoService.list(elevatorInfo,collect);
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存电梯
     */
    @RequiresPermissions("business:elevatorInfo:add")
    @Log(title = "电梯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result addSave(ElevatorInfo elevatorInfo) {
        if (StringUtils.isNotEmpty(elevatorInfo.getLongitude())) {
            Console.log("经纬度:"+elevatorInfo.getLongitude());
            String[] split = elevatorInfo.getLongitude().split(",");
            elevatorInfo.setLongitude(split[0]);
            elevatorInfo.setLatitude(split[1]);
        }
        elevatorInfo.setCreateTime(new Date());
        elevatorInfo.setCreateBy(ShiroUtils.getLoginName());
        elevatorInfoService.save(elevatorInfo);

        //保存保养计划
        UpkeepPlan upkeepPlan = new UpkeepPlan();
        upkeepPlan.setElevatorId(elevatorInfo.getElevatorId());
        upkeepPlan.setFirstUpkeepTime(DateUtil.parse(DateUtils.getISO8601Time(elevatorInfo.getCreateTime()), "yyyy-MM-dd").toString());
        upkeepPlan.setUpkeepType("1");//默认半月保养
        upkeepPlan.setRemindDay(5);//默认5天前提醒
        upkeepPlan.setUpkeepInterval(15);//默认半月15天间隔
        upkeepPlan.setCreateTime(DateUtil.date());
        upkeepPlan.setCreateBy(ShiroUtils.getLoginName());
        upkeepPlanService.save(upkeepPlan);
        return Result.success();
    }

    /**
     * 修改保存电梯
     */
    @RequiresPermissions("business:elevatorInfo:edit")
    @Log(title = "电梯", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(ElevatorInfo elevatorInfo) {
        //安装电梯 安装日期==年检日期,,,,,需求确定:不知道年检周期是多少,直接选择下次年检计划
        elevatorInfo.setAnnualDate(DateUtil.date());
        elevatorInfoService.updateById(elevatorInfo);
        return Result.success();
    }

    /**
     * 删除电梯
     */
    @RequiresPermissions("business:elevatorInfo:remove")
    @Log(title = "电梯", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        elevatorInfoService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出电梯列表
     */
    @RequiresPermissions("business:elevatorInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(ElevatorInfo elevatorInfo) {
        List<CommunityManage> communityList = userService.findCommunityByUserId(ShiroUtils.getUserId());
        if (CollUtil.isEmpty(communityList)){
            return Result.success();
        }
        List<Long> collect = communityList.stream().map(CommunityManage::getCommunityId).collect(Collectors.toList());
        List<ElevatorInfo> list = elevatorInfoService.list(elevatorInfo,collect);
        ExcelUtil<ElevatorInfo> util = new ExcelUtil<>(ElevatorInfo.class);
        util.exportExcel(list, "elevatorInfo");
        return Result.success();
    }
}
